// 文件上传
const multer = require('multer');
const path = require('path');
const fs = require('fs');
 
const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    // 指定上传文件的文件夹路径
    const uploadFolder = 'public/uploads';
    cb(null, uploadFolder);
  },
  filename: (req, file, cb) => {
    // 生成唯一的文件名，这里使用时间戳作为文件名
    const timestamp = Date.now();
    const extension = path.extname(file.originalname);
    const filename = `${timestamp}${extension}`;
    cb(null, filename);
  },
});
 
const upload = multer({ storage: storage }).single('file');
 
function uploadFile(req, res, next) {
  upload(req, res, next, async function (err) {
    if (err) {
      console.log(err);
      res.status(500).send({ success: false, message: '文件上传失败' });
    } else {
      next(); // 调用下一个中间件或路由处理函数
    }
  });
}
 
module.exports = uploadFile;